import { VerticalBox, HorizontalBox, Button, ListView, ProgressIndicator } from "std-widgets.slint";

// 全局事件定义
export global AppEvents {
    // 安装请求信号
    in-out callback install-requested(string);
    
    // 进度更新信号
    in-out callback progress-updated(float);
    
    // 错误提示信号
    in-out callback show-error(string);
}

// 主窗口组件
export component MainWindow inherits Window {
    // 当前选中的包类型
    in-out property <string> selected-pkg-type: "linglong";
    
    // 包列表数据模型
    in-out property <[PackageInfo]> package-list;
    
    // 安装进度值
    in-out property <float> installation-progress: 0.0;
    
    // 界面布局
    VerticalBox {
        // 顶部导航栏
        HorizontalBox {
            spacing: 10px;
            height: 40px;
            
            // 玲珑包标签
            ToggleButton {
                text: "玲珑包";
                checked: root.selected-pkg-type == "linglong";
                toggled => { root.selected-pkg-type = "linglong"; }
            }
            
            // Flatpak标签  
            ToggleButton {
                text: "Flatpak";
                checked: root.selected-pkg-type == "flatpak";
                toggled => { root.selected-pkg-type = "flatpak"; }
            }
            
            // Snap标签
            ToggleButton {
                text: "Snap";
                checked: root.selected-pkg-type == "snap";
                toggled => { root.selected-pkg-type = "snap"; }
            }
        }

        // 包列表区域
        ListView {
            height: 300px;
            for pkg in root.package-list: ListItem {
                HorizontalBox {
                    spacing: 8px;
                    width: 100%;
                    
                    // 包名称
                    Text { text: pkg.name; horizontal-alignment: left; }
                    
                    // 版本号
                    Text { text: pkg.version; }
                    
                    // 安装状态
                    Text { 
                        text: pkg.installed ? "已安装" : "未安装";
                        color: pkg.installed ? green : red; 
                    }
                    
                    // 操作按钮
                    Button {
                        text: pkg.installed ? "卸载" : "安装";
                        enabled: root.installation-progress == 0;
                        clicked => {
                            if (!pkg.installed) {
                                AppEvents.install-requested(pkg.file-path);
                            }
                        }
                    }
                }
            }
        }

        // 进度指示器
        ProgressIndicator {
            visible: root.installation-progress > 0;
            percentage: root.installation-progress * 100;
        }

        // 底部状态栏
        HorizontalBox {
            height: 30px;
            Text { text: "就绪"; }
        }
    }

    // 错误提示弹窗
    PopupWindow {
        visible: false;
        Text { text: "发生错误: "; }
    }
}

// 列表项数据绑定
struct PackageInfo {
    name: string,
    version: string,
    installed: bool,
    file-path: string,
}